/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "interface/optical_module.h"

namespace dev {

bool OpticalModule::get_optical_module_info_by_ncsi() {
    ncsi_optical_module_info_t optical_module_info = {};
    bool ret = m_ncsi_over_mctp_huawei->get_optical_module_info(DEFAULT_PACKAGE_ID, m_port_id,
                                                                &optical_module_info);
    if (!ret) {
        elog("Error: Failed to get optical module info");
        return false;
    }
    auto manufacturer  = std::string(reinterpret_cast<char*>(optical_module_info.manufacturer),
                                     sizeof(optical_module_info.manufacturer));
    auto part_number   = std::string(reinterpret_cast<char*>(optical_module_info.part_number),
                                     sizeof(optical_module_info.part_number));
    auto serial_number = std::string(reinterpret_cast<char*>(optical_module_info.serial_number),
                                     sizeof(optical_module_info.serial_number));

    // 去除尾部的空格
    manufacturer.erase(manufacturer.find_last_not_of(' ') + 1);
    part_number.erase(part_number.find_last_not_of(' ') + 1);
    serial_number.erase(serial_number.find_last_not_of(' ') + 1);

    Presence.set_value(1);
    Manufacturer.set_value(manufacturer);
    PartNumber.set_value(part_number);
    SerialNumber.set_value(serial_number);
    MediumType.set_value(optical_module_info.medium_type);
    ConnectorType.set_value(optical_module_info.connector_type);
    TransmissionDistance.set_value(optical_module_info.transceiver_distance);
    if (optical_module_info.medium_type == "Optical" ||
        optical_module_info.medium_type == "Aoc" ||
        optical_module_info.medium_type == "Electric") {
        WaveLengthNanometer.set_value(optical_module_info.wave_length_nanometer);
        Identifier.set_value(optical_module_info.identifier);
    }
    return true;
}

void OpticalModule::start_ncsi_update_task_huawei(ncsi_over_mctp_hw_ptr ncsi_over_mctp,
                                                  mc::milliseconds      interval) {
    m_ncsi_over_mctp_huawei = ncsi_over_mctp;
    m_ncsi_timer            = new mc::core::timer(this);
    m_ncsi_timer->timeout.connect([this]() {
        get_optical_module_info_by_ncsi();
    });
    m_ncsi_timer->set_single_shot(false);
    m_ncsi_timer->start(interval);
}

void OpticalModule::stop_ncsi_update_task() {
    if (m_ncsi_timer) {
        m_ncsi_timer->stop();
        delete m_ncsi_timer;
        m_ncsi_timer = nullptr;
    }
    if (m_ncsi_over_mctp_huawei) {
        m_ncsi_over_mctp_huawei = nullptr;
    }
}

} // namespace dev

MC_REFLECT(dev::OpticalModule)
